package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.Class;
import com.zyg.service.ClassService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/class")
public class ClassController {

    @Autowired
    private ClassService classService;

    @RequestMapping("/tolist")
    public String tolist() {
        return "class/list";
    }

    @RequestMapping("/list")
    @ResponseBody
    public String list(Class clazz,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit) {
        QueryWrapper<Class> queryWrapper = new QueryWrapper<>();
        if (clazz.getClassName() != null && !clazz.getClassName().isEmpty()) {
            queryWrapper.like("class_name", clazz.getClassName());
        }
        if (clazz.getGradeId() != null) {
            queryWrapper.eq("grade_id", clazz.getGradeId());
        }

        long totalCount = classService.count(queryWrapper);
        Page<Class> page1 = new Page<>(page, limit);
        List<Class> classList = classService.page(page1, queryWrapper).getRecords();

        LayData layData = new LayData();
        layData.setCount((int) totalCount);
        layData.setData(classList);
        layData.setCode(0);
        layData.setMsg("查询成功");

        return JSON.toJSONString(layData);
    }

    @RequestMapping("/toadd")
    public String toadd() {
        return "class/add";
    }

    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(@ModelAttribute Class clazz) {
        try {
            boolean result = classService.save(clazz);
            return JSON.toJSONString(new LayData(result ? 0 : 1, result ? "保存成功" : "保存失败"));
        } catch (Exception e) {
            e.printStackTrace();
            return JSON.toJSONString(new LayData(1, "保存失败: " + e.getMessage()));
        }
    }

    @RequestMapping("/toupdate/{id}")
    public String toUpdate(@PathVariable("id") int id, Model model) {
        Class clazz = classService.getById(id);
        model.addAttribute("clazz", clazz);
        return "class/update";
    }

    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Class clazz) {
        boolean result = classService.updateById(clazz);
        return JSON.toJSONString(new LayData(result ? 0 : 500, result ? "更新成功" : "更新失败"));
    }

    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id") int id) {
        boolean result = classService.removeById(id);
        return JSON.toJSONString(new LayData(result ? 0 : 500, result ? "删除成功" : "删除失败"));
    }
}
